//
//  TSMutipleCheckTableViewController.m
//  chart
//
//  Created by xu on 15/8/2.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSMutipleCheckTableViewController.h"
#import "TSMultipleCheckCell.h"
#import "TSRecieverListModel.h"

@interface TSMutipleCheckTableViewController ()

@end

@implementation TSMutipleCheckTableViewController
{
    NSArray *rowsArray;
    NSMutableArray *rowsSelectedArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TSRecieverListModel *model = [[TSRecieverListModel alloc] init];
    model.requestParams = @{@"pageIndex":@(0), @"pageSize":@(10)};
    WS(weakSelf);
    [model fetchWithPath:kRequestUrl_SendMessage_outList_ableMessage completionHandler:^(BOOL isSucceeded, NSError *error) {
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (isSucceeded) {
            
            strongSelf->rowsArray = model.list;
            [weakSelf createRowsSelectedArray:model.list.count];
            
            [weakSelf.tableView reloadData];
        }
    } NoProgress:NO];

    
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(onCloseVC)];
    [self.navigationItem setLeftBarButtonItem:barButtonItem];
    
    barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(onSelectCompelete)];
    [self.navigationItem setRightBarButtonItem:barButtonItem];
}

- (void)createRowsSelectedArray:(NSUInteger)count
{
    rowsSelectedArray = [[NSMutableArray alloc] initWithCapacity:rowsArray.count];
    for (int i=0; i<count; i++) {
        [rowsSelectedArray addObject:[NSNumber numberWithBool:NO]];
    }
}

- (void)onCloseVC
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/* 多选后完成通知出去 */
- (void)onSelectCompelete
{
    [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_OnMultipleSelectCompelete object:[self findAllYesNames]];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSString *)findAllYesNames
{
    NSMutableString *yes_names = [[NSMutableString alloc] initWithString:@""];
    
    for (int i=0; i<rowsSelectedArray.count; i++) {
        NSNumber *selected = [rowsSelectedArray objectAtIndex:i];
        if (selected.boolValue) {
            if (rowsArray && rowsArray.count > i) {
                TSUserModel *model = [rowsArray objectAtIndex:i];
                [yes_names appendString:[NSString stringWithFormat:@"%@,", model.userName]];
            }
        }
    }
    
    if (yes_names.length > 0) {
        yes_names = [NSMutableString stringWithString:[yes_names substringToIndex:yes_names.length-1]];
    }
    
    return yes_names;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [rowsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TSMultipleCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    if (!cell) {
        cell = [[TSMultipleCheckCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    }
    
    [self removeAllSubviews:cell];
    
    if (rowsArray.count > indexPath.row) {
        TSUserModel *model = [rowsArray objectAtIndex:indexPath.row];
        
        NSNumber *is_selected = [rowsSelectedArray objectAtIndex:indexPath.row];
        
        UIImageView *image_view = [self createImageView:is_selected.boolValue?@"cell_selected":@"cell_not_selected"];
        [cell.contentView addSubview:image_view];
        
        UILabel *label = [self createLabel:model.userName];
        [cell.contentView addSubview:label];
        
        
    }
    
    return cell;
}

- (void)removeAllSubviews:(UITableViewCell *)cell
{
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
}

- (UIImageView *)createImageView:(NSString *)imageName
{
    UIImageView *image_view = [[UIImageView alloc] initWithFrame:CGRectMake(MARGIN_LEFT*2, MARGIN_TOP, 30.f, 30.f)];
    [image_view setImage:[UIImage imageNamed:imageName]];
    
    return image_view;
}

- (UILabel *)createLabel:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(MARGIN_LEFT*6, MARGIN_TOP, currentDeviceWidth-MARGIN_LEFT*7, 30.f)];
    [label setText:title];
    
    return label;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIImageView *view = [self findImageViewInCell:cell];
    if ([view.image isEqual:[UIImage imageNamed:@"cell_not_selected"]]) {
        [view setImage:[UIImage imageNamed:@"cell_selected"]];
        
        [rowsSelectedArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
    }
    else{
        [view setImage:[UIImage imageNamed:@"cell_not_selected"]];
        [rowsSelectedArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
    }
    
}

- (UIImageView *)findImageViewInCell:(UITableViewCell *)cell
{
    for (UIView *view in cell.contentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            return (UIImageView *)view;
        }
    }
    
    return nil;
}


@end
